<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请人" prop="applicat">
        <el-input
          v-model="queryParams.applicat"
          placeholder="请输入申请人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人电话" prop="applicatPhone" label-width="100px">
        <el-input
          v-model="queryParams.applicatPhone"
          placeholder="请输入申请人电话"
          clearable
          size="medium"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="疫苗类型" prop="vaccineType">
        <el-select v-model="queryParams.vaccineType" placeholder="请选择疫苗申请类型" clearable size="small">
          <el-option label="口蹄疫苗" value="1"></el-option>
          <el-option label="布菌疫苗" value="2"></el-option>
          <el-option label="炭菌疫苗" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable size="small">
          <el-option label="已审核" value="1"></el-option>
          <el-option label="未审核" value="2"></el-option>
          <el-option label="驳回" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['dgw:applicat:add']"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['dgw:applicat:edit']"
              >修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['dgw:applicat:remove']"
              >删除</el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['dgw:applicat:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applicatList" @selection-change="handleSelectionChange">


      <el-table-column toggleRowExpansion="row" fixed type="expand">
        <template slot-scope="scope">
          <el-table :data="scope.row.vaccineList" style="width: 100%"
                    height="400px"
                    :row-class-name="tableRowClassName">
            <el-table-column label="耳标编号" align="center" prop="earNumber"/>
<!--            <el-table-column label="温度" align="center" prop="temp">
              <template slot-scope="scope">
                <el-tag effect="dark" size="small" :type="scope.row.temp< 20?'danger': 27<=scope.row.temp <=38?'primary':'danger'">
                  {{scope.row.temp}}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="运动量" align="center" prop="jbu"/>
            <el-table-column label="电量" align="center" prop="power">
              <template slot-scope="scope">
                <el-tag effect="dark" size="small" :type="scope.row.power >=70?'primary': 50<scope.row.power <70?'warning':'danger'">
                  {{scope.row.power}}
                </el-tag>
              </template>
            </el-table-column>

            <el-table-column label="是否拆卸" align="center" prop="fcai">
              <template slot-scope="scope">
                <el-tag :type="scope.row.fcai==0?'primary':'danger'">
                  {{scope.row.fcai==0?'未拆卸':'已拆卸'}}
                </el-tag>
              </template>
            </el-table-column>-->
<!--            <el-table-column label="用户手机号" align="center" prop="phone"/>-->
<!--            <el-table-column label="牛品种" align="center" prop="cowType">
              <template slot-scope="scope">
                <span>{{scope.row.cowType==1?'育肥牛':scope.row.cowType==2?'牛犊':scope.row.cowType==3?'奶牛':'母牛'}}</span>
              </template>
            </el-table-column>-->
<!--            <el-table-column label="牛舍名称" align="center" prop="cowPenName"/>
            <el-table-column label="牛棚名称" align="center" prop="cowShedName"/>
            <el-table-column label="牛棚地址" align="center" prop="shedAddress" width="300"/>-->
          </el-table>
        </template>
      </el-table-column>




<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="乡镇申请人" align="center" prop="applicat"/>
      <el-table-column label="申请时间" align="center" prop="applicatTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applicatTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="applicatPhone"/>
      <el-table-column label="疫苗类型" align="center" prop="vaccineType">
        <template slot-scope="scope">
          <span>{{ scope.row.vaccineType == 1 ? '口蹄疫苗' : scope.row.vaccineType == 2 ? '布菌疫苗' : '炭菌疫苗' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="applicatSum"/>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status==1?'info':scope.row.status==2?'primary':'danger'">
            {{ scope.row.status == 1 ? '未审核' : scope.row.status == 2 ? '审核通过' : '驳回' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="县审核人" align="center" prop="statusBy"/>
      <el-table-column label="审核时间" align="center" prop="statusTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.statusTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="驳回原因" align="center" prop="cause"/>
      <!--      <el-table-column label="用户id" align="center" prop="userId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleUpdateStatus(scope.row)" round>审核</el-button>

          <!--          <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      v-hasPermi="['dgw:applicat:edit']"
                    >修改</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      v-hasPermi="['dgw:applicat:remove']"
                    >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改疫苗申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请人" prop="applicat">
          <el-input v-model="form.applicat" placeholder="请输入申请人"/>
        </el-form-item>
        <el-form-item label="申请人电话" prop="applicatPhone">
          <el-input v-model="form.applicatPhone" placeholder="请输入申请人电话"/>
        </el-form-item>
        <el-form-item label="申请数量" prop="applicatSum">
          <el-input v-model="form.applicatSum" placeholder="请输入申请数量"/>
        </el-form-item>
        <el-form-item label="疫苗申请类型，1=口蹄疫苗，2=布菌疫苗，3=炭菌疫苗" prop="vaccineType">
          <el-select v-model="form.vaccineType" placeholder="请选择疫苗申请类型，1=口蹄疫苗，2=布菌疫苗，3=炭菌疫苗">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间" prop="applicatTime">
          <el-date-picker clearable size="small"
                          v-model="form.applicatTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择申请时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核状态，1=已审核，2=未审核，3=驳回">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核人" prop="statusBy">
          <el-input v-model="form.statusBy" placeholder="请输入审核人"/>
        </el-form-item>
        <el-form-item label="审核时间" prop="statusTime">
          <el-date-picker clearable size="small"
                          v-model="form.statusTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择审核时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="驳回原因" prop="cause">
          <el-input v-model="form.cause" placeholder="请输入驳回原因"/>
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openStatus" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80pox">
        <el-form-item label="处理人" prop="statusBy">
          <el-input v-model="form.statusBy" placeholder="请输入处理人"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormStatus">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listApplicat,
  getApplicat,
  delApplicat,
  addApplicat,
  updateApplicat,
  exportApplicat,
  updateStatus
} from '@/api/dgw/applicat'
import DraggableItem from '@/views/tool/build/DraggableItem'

export default {
  name: 'Applicat',
  components: { DraggableItem },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 疫苗申请表格数据
      applicatList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      //审核弹出层
      openStatus: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicat: null,
        applicatPhone: null,
        vaccineType: null,
        status: null
      },
      // 表单参数
      form: {},
      formStatus: {},
      // 表单校验
      rules: {
        statusBy: [
          { required: true, message: "审批人不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询疫苗申请列表 */
    getList() {
      this.loading = true
      listApplicat(this.queryParams).then(response => {
        console.log("查询疫苗申请列表",response)
        this.applicatList = response.rows
        this.total = response.rows.length
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openStatus = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applicat: null,
        applicatPhone: null,
        applicatSum: null,
        vaccineType: null,
        applicatTime: null,
        status: 0,
        statusBy: null,
        statusTime: null,
        cause: null,
        userId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加疫苗申请'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getApplicat(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改疫苗申请'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApplicat(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addApplicat(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除疫苗申请编号为"' + ids + '"的数据项？').then(function() {
        return delApplicat(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal.confirm('是否确认导出所有疫苗申请数据项？').then(() => {
        this.exportLoading = true
        return exportApplicat(queryParams)
      }).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    },
    handleUpdateStatus(row) {
      console.log('row', row)
      this.reset()
      if (row.status == 3) {
        this.formStatus = false
        this.$message.error('该信息已被驳回,不可审核')
      } else if (row.status == 2) {
        this.formStatus = false
        this.$message.error('该信息审核已通过，不可重复审核')
      } else {
        const id = row.id
        this.openStatus = true
        this.title = '审核批准'
        this.form.id = id
      }

      /* this.formStatus.statusBy = row.*/
      /*updateStatus(id).then(response=>{
      })*/
    },
    submitFormStatus() {
      this.$refs['form'].validate(valid => {
        console.log('form', this.form)
        if (valid) {
          updateStatus(this.form).then(response => {
            this.$modal.msgSuccess('审核成功')
            this.openStatus = false
            this.getList()
          })
        }
      })
    }

  }
}
</script>
